//
//  BBActivityDetailViewController.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/11/26.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import MapKit
import UIKit

class BBActivityDetailViewController: UIViewController {
    @IBOutlet var mapView: MKMapView!
    @IBOutlet var deviceName: UILabel!
    @IBOutlet var distanceLabel: UILabel!
    @IBOutlet var timeLabel: UILabel!
    @IBOutlet var aveSpeedLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        drawLine()
        let distanceK = (activity?.distance ?? 0) / 1000
        let distanceM = ((activity?.distance ?? 0) % 1000) / 100
        distanceLabel.text = "\(distanceK).\(distanceM)Km"
        aveSpeedLabel.text = "\(Double((activity?.speedAvg)!) * 3.6)km/h"
        timeLabel.text = "\((activity?.usedTime)! / 3600)H,\(((activity?.usedTime)! % 3600) / 60)m,\((activity?.usedTime)! % 60)s"
        deviceName.text = BBUser.loadUser()?.devices?.first?.deviceName
    }

    var activity: BBActivity? {
        didSet {
            locationCoordinate2Ds = transCLLocationCoordinate2D(latLonStr: activity?.activityLines)
        }
    }

    var locationCoordinate2Ds: [CLLocationCoordinate2D]?

    private func drawLine() {
        if locationCoordinate2Ds != nil {
            let polyline: MKPolyline = MKPolyline(coordinates: locationCoordinate2Ds!, count: locationCoordinate2Ds!.count)
            mapView.addOverlay(polyline)
            let coor1 = locationCoordinate2Ds!.first
            let coor2 = locationCoordinate2Ds!.last
            let coor = locationCoordinate2Ds![locationCoordinate2Ds!.count / 2]
            let distance: Double = distanceBetweenOrderBy(latitude1: (coor1?.latitude)!, longitude1: (coor1?.longitude)!, latitude2: (coor2?.latitude)!, longitude2: (coor2?.longitude)!)
            mapView.setRegion(MKCoordinateRegion(center: coor, latitudinalMeters: distance + 2000, longitudinalMeters: distance + 2000), animated: true)
        }
    }

    private func distanceBetweenOrderBy(latitude1: CLLocationDegrees, longitude1: CLLocationDegrees, latitude2: CLLocationDegrees, longitude2: CLLocationDegrees) -> Double {
        let dd: Double = M_PI / 180
        let x1: Double = latitude1 * dd
        let x2: Double = latitude2 * dd
        let y1: Double = longitude1 * dd
        let y2: Double = longitude2 * dd
        let R: Double = 6371004
        let distance: Double = (2 * R * asin(sqrt(2 - 2 * cos(x1) * cos(x2) * cos(y1 - y2) - 2 * sin(x1) * sin(x2)) / 2))
        return distance
    }

    private func transCLLocationCoordinate2D(latLonStr: String?) -> [CLLocationCoordinate2D]? {
        if latLonStr != nil {
            let lonAndLatList = latLonStr!.components(separatedBy: ",")
            var locs: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
            for str in lonAndLatList {
                let arr = str.components(separatedBy: ":")
                let longitude = arr.first
                let latitude = arr.last
                let coor = CLLocationCoordinate2D(latitude: Double(latitude!)!, longitude: Double(longitude!)!)
                locs.append(coor)
            }
            return locs
        }
        return nil
    }
}

extension BBActivityDetailViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let polylineRenderer = MKPolylineRenderer(overlay: overlay)
        polylineRenderer.strokeColor = UIColor.green
        polylineRenderer.lineWidth = 3
        return polylineRenderer
    }
}
